package com.example.music.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.example.music.R;
import com.example.music.databinding.DialogCustomBinding;


public class CustomDialog extends Dialog {


    public CustomDialog(Context context, int theme) {
        super(context,theme);
    }

    public static class Builder {
        private Context context; //上下文对象
        private String title; //对话框标题
        private String message; //对话框内容
        private String confirm_btnText; //按钮名称“确定”
        private String cancel_btnText; //按钮名称“取消”
        private View contentView; //对话框中间加载的其他布局界面
        //按钮监听事件
        private OnClickListener confirm_btnClickListener;
        private OnClickListener cancel_btnClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        //设置对话框信息
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        //设置标题信息
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        //设置对话框界面
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        //设置肯定按钮
        public Builder setPositiveButton(String confirm_btnText,
                                         OnClickListener listener) {
            this.confirm_btnText = confirm_btnText;
            this.confirm_btnClickListener = listener;
            return this;
        }

        //设置否定按钮
        public Builder setNegativeButton(String cancel_btnText,
                                         OnClickListener listener) {
            this.cancel_btnText = cancel_btnText;
            this.cancel_btnClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.MyDialogStyle);
            DialogCustomBinding binding = DialogCustomBinding.inflate(inflater);
            dialog.addContentView(binding.getRoot(), new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT));
            // 设置内容
            if (message != null) {
                binding.title.setText(title);
                binding.title.getPaint().setFakeBoldText(true);
            }else {
                binding.title.setVisibility(View.GONE);
            }
            // 设置确定按钮
            if (confirm_btnText != null) {
                binding.confirmBtn.setText(confirm_btnText);
                if (confirm_btnClickListener != null) {
                    binding.confirmBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            confirm_btnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                binding.confirmBtn.setVisibility(View.GONE);
                binding.message.setVisibility(View.GONE);
            }
            // 设置取消按钮
            if (cancel_btnText != null) {
                binding.cancelBtn.setText(cancel_btnText);
                if (cancel_btnClickListener != null) {
                    binding.cancelBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            cancel_btnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                binding.cancelBtn.setVisibility(View.GONE);
                binding.btnView.setVisibility(View.GONE);
            }
            // 设置内容
            if (message != null) {
                binding.message.setText(message);
            } else if (contentView != null) {
                binding.llMessage.removeAllViews();
                binding.llMessage.addView(
                        contentView, new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
            }
            //设置对话框大小
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);
            dialog.setContentView(binding.getRoot());
            return dialog;
        }

    }
}
